package com.thinkmore.es.utils;

import org.nlpcn.commons.lang.pinyin.Pinyin;

import java.util.List;
import java.util.regex.Pattern;

public class PinyinHelper {

    /**
     * word and number
     */
    static String reg = "^[A-Za-z\\d]$";

    /**
     * 获取拼音
     * @param text
     */
    public static String getPinYin(String text) {
        List<String> pinyin = Pinyin.pinyin(text);
        /*List<String> pinyin = Pinyin.firstChar(text);
        System.out.println(pinyin);*/
        StringBuilder sb = new StringBuilder();
        String[] split = text.split("");
        for (int i = 0; i < pinyin.size(); i++) {
            String word = pinyin.get(i);
            if (word == null) {
                if (Pattern.matches(reg, split[i])) {
                    sb.append(split[i]);
                }
            } else {
                sb.append(word);
            }
        }
        return sb.toString();
    }
}
